﻿using Help.Library.Help;
using PokemonClientToos.Entity;
using PokemonTools;
using PokemonTools.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PokemonClientToos.ActionFile
{
    public class ActionCall
    {
        private ActionCall() { }
        public static readonly ActionCall instance = new ActionCall();

        public bool Action400(string acount, string password)
        {
            // 加密密码
            string md5pw = HelpSecurity.GetMD5(string.Format("{0}{1}{2}", "ccw", password, "15806032932"));

            string postDataStr = string.Format("ActionID=400&sdkType={0}&acountOrSdkUid={1}&password={2}&gameId={3}&serverId={4}"
                , 0, acount, md5pw, 1, 1002);

            Robot.instance.HttpGet(@"http://192.168.3.251:10100/", postDataStr);

            Bytebuffer buffer = new Bytebuffer(true);
            buffer.ActionId = 400;
            GameRole.dataBytes.CopyTo(buffer.Data, 0);
            buffer.ReadHead();

            if (buffer.IsErrr)
            {
                System.Windows.Forms.MessageBox.Show(buffer.ReadString());
                return false;
            }
            GameRole.loginInfo.Password = md5pw;
            GameRole.loginInfo.ServerIP = buffer.ReadString();
            GameRole.loginInfo.ServerTcpPort = buffer.ReadInt();
            GameRole.loginInfo.Uid = buffer.ReadInt();
            GameRole.loginInfo.Rid = buffer.ReadInt();
            GameRole.loginInfo.Token = buffer.ReadString();
            GameRole.loginInfo.Acount = buffer.ReadString();

            Robot.instance.PutMessage(string.Format("账号[{0}]登录成功,角色Id[{1}]", acount, GameRole.loginInfo.Rid));
            return true;
        }

        public void Action1000(Action call = null)
        {
            Bytebuffer buffer = new Bytebuffer(false);
            buffer.ActionId = 1000;
            buffer.WriteInt(0);
            buffer.WriteInt(1);
            buffer.WriteInt(0);
            buffer.WriteString(GameRole.loginInfo.Acount);
            buffer.WriteString(GameRole.loginInfo.Password);
            buffer.WriteInt(GameRole.loginInfo.Uid);
            buffer.WriteInt(GameRole.loginInfo.Rid);
            buffer.WriteString(GameRole.loginInfo.Token);
            buffer.WriteString("cn");
            buffer.WriteByte(0);
            buffer.WriteString("");
            buffer.WriteInt(10);
            // 发送请求
            Robot.instance.TcpPost(buffer.GetPostArray());
            // 回调
            CallbackAction callBack = new CallbackAction();
            callBack.SetAction(delegate()
            {
                GameRole.loginInfo.Sid = callBack.Buffer.ReadInt();
                if (call != null)
                    call();
            });

            if (!Robot.callCache.ContainsKey(buffer.ActionId))
            {
                Robot.callCache.Add(buffer.ActionId, callBack);
            }
        }

        public void ActionTcp(Bytebuffer buffer, Action call = null)
        {
            // 发送请求
            Robot.instance.TcpPost(buffer.GetPostArray());
            // 回调
            CallbackAction callBack = new CallbackAction();
            callBack.SetAction(delegate()
            {
                if (call != null)
                {
                    call();
                }
            });

            if (!Robot.callCache.ContainsKey(buffer.ActionId))
            {
                Robot.callCache.Add(buffer.ActionId, callBack);
            }
        }
    }
}
